#ifndef __ZGH__
#define __ZGH__

#include <iostream>
#include <execinfo.h>
#include <vector>
#include <sstream>
#include "demo04.cpp"


void Backtrace(std::vector<std::string>& bt, int size, int skip){
    void** array = (void**)malloc(sizeof(void*)*size);
    size_t s = ::backtrace(array, size);
    char** strings = ::backtrace_symbols(array, s);
    if(strings == NULL){
        std::cout<<"backtrace_synbols error"<<std::endl;
        return;
    }
    for(size_t i = skip;i < s;i++){
        bt.push_back(strings[i]);
    }
    free(strings);
    free(array);
}

std::string BacktraceToString(int size, int skip, const std::string& prefix){
    std::vector<std::string> bt;
    Backtrace(bt, size, skip);
    std::stringstream ss;
    for(size_t i = 0; i< bt.size();i++){
        ss<<prefix<<bt[i]<<std::endl;
    }
    return ss.str();
}

//普通断言 传入布偶值
#define SYLAR_ASSERT(x) \
    if(!(x)) { \
        std::cout << "ASSERTION: " #x \
            << "\nbacktrace:\n" \
            << sylar::BacktraceToString(100, 2, "    "); \
        assert(x); \
    }

//额外断言 可传入额外信息进行辅助输出
#define SYLAR_ASSERT2(x, w) \
    if(!(x)) { \
        std::cout << "ASSERTION: " #x \
            << "\n" << w \
            << "\nbacktrace:\n" \
            << sylar::BacktraceToString(100, 2, "    "); \
        assert(x); \
    }

#endif

